package utils

import (
	"time"
)

type IntervalSleep struct {
	interval int
	s        time.Time
	elapsed  int
	sleep    int
}

func NewIntervalSleep(i int) *IntervalSleep {
	return &IntervalSleep{interval: i}
}

func (m *IntervalSleep) Start() {
	m.s = time.Now()
}

func (m *IntervalSleep) Sleep() {
	m.elapsed = int(float64(time.Since(m.s)) * 0.000001)

	if m.elapsed >= m.interval {
		m.sleep = 0
	} else {
		m.sleep = m.interval - m.elapsed
	}

	time.Sleep(time.Millisecond * time.Duration(m.sleep))
}
